﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AcademicPlanner.Domain.Models;

namespace AcademicPlanner.Presentation.Views.Shared
{
	/// <summary>
	/// Possible scenarios of a <c>ProgramForm</c> form.
	/// </summary>
	/// <since>1.0</since>
	public enum ProgramFormScenario
	{
		CREATE,
		EDIT
	}


	/// <summary>
	/// ProgramFormModel represents a model object for a <c>ProgramForm</c> form.
	/// </summary>
	/// <since>1.0</since>
	public class ProgramFormModel : FormModel
	{
		/// <summary>
		/// Gets or sets the scenario to use the <c>ProgramForm</c> form under.
		/// </summary>
		/// <since>1.0</since>
		public ProgramFormScenario Scenario { get; set; }

		/// <summary>
		/// Gets or sets the <c>Program</c> whose data to save.
		/// </summary>
		/// <remarks>
		/// This property is not used in <c>CREATE</c> scenario.
		/// </remarks>
		/// <since>1.0</since>
		public Program Program { get; set; }

		/// <summary>
		/// Gets or sets the identifier of the <c>AcademicPlan</c> the
		/// academic program belongs to.
		/// </summary>
		/// <remarks>
		/// This property is used in <c>CREATE</c> scenario only.
		/// </remarks>
		/// <since>1.0</since>
		public int PlanId { get; set; }

		/// <summary>
		/// Gets or sets the <c>Course</c>s of the academic plan 
		/// the <c>Program</c> belongs to.
		/// </summary>
		/// <remarks>
		/// This property is used in <c>CREATE</c> scenario only.
		/// </remarks>
		/// <since>1.0</since>
		public IList<Course> PlanCourses { get; set; }
	}


	/// <summary>
	/// ProgramForm represents a form control to save a <c>Program</c> data.
	/// </summary>
	/// <since>1.0</since>
	public partial class ProgramForm : Form<ProgramFormModel>
	{
		/// <summary>
		/// Gets the model object for this form control.
		/// </summary>
		/// <since>1.0</since>
		public new ProgramFormModel Model { get { return (ProgramFormModel)base.Model; } }

		/// <summary>
		/// Gets or sets the model object for the <c>CoursesControl</c> control contained in this form
		/// for selection of required courses.
		/// </summary>
		/// <since>1.0</since>
		public CoursesControlModel SelectRequiredCoursesModel { get; set; }

		/// <summary>
		/// Gets or sets the model object for the <c>CoursesControl</c> control contained in this form
		/// for selection of recommended courses.
		/// </summary>
		/// <since>1.0</since>
		public CoursesControlModel SelectRecommendedCoursesModel { get; set; }

		/// <summary>
		/// Gets or sets the value of the minimum credits field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string MinimumCreditsValue { get; set; }

		/// <summary>
		/// Gets or sets the value of the name field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string NameValue { get; set; }


		/// <summary>
		/// Handles the page load event for this form.
		/// </summary>
		/// <remarks>
		/// This methods intializes or customizes the form's properties
		/// based on the model object's <c>Scenario</c>.
		/// </remarks>
		/// <param name="sender">The sender of the event.</param>
		/// <param name="e">The details about the event.</param>
		/// <since>1.0</since>
		protected void Page_Load(object sender, EventArgs e)
		{
			method = FormMethod.Post;

			if (Model.Scenario.Equals(ProgramFormScenario.EDIT))
			{
				actionName = "Save";
				controllerName = "Programs";
				routeValues = new { pId = Model.Program.EntityId };
				Title = Tr("Edit Program");
				Name = "editProgramForm";
				SubmitButtonValue = Tr("Save");
			}
			else
			{
				actionName = "Save";
				controllerName = "Programs";
				routeValues = new { pId = 0, pParentId = Model.PlanId };
				Title = Tr("Create Program");
				Name = "createProgramForm";
				SubmitButtonValue = Tr("Save");
			}

			if (string.IsNullOrEmpty(Model.Id)) Model.Id = Name;

			SelectRequiredCoursesModel = new CoursesControlModel();
			SelectRecommendedCoursesModel = new CoursesControlModel();
			SelectRequiredCoursesModel.Mode = CoursesControlMode.SELECT;
			SelectRecommendedCoursesModel.Mode = CoursesControlMode.SELECT;
			SelectRequiredCoursesModel.Id = Model.Id + "RequiredCourses";
			SelectRecommendedCoursesModel.Id = Model.Id + "RecommendedCourses";
			SelectRequiredCoursesModel.SelectedCoursesFieldName = GetFieldName("RequiredCourses");
			SelectRecommendedCoursesModel.SelectedCoursesFieldName = GetFieldName("RecommendedCourses");
			SelectRequiredCoursesModel.Courses = Model.PlanCourses;
			SelectRecommendedCoursesModel.Courses = Model.PlanCourses;

			if (Model.Scenario.Equals(ProgramFormScenario.EDIT))
			{
				NameValue = Model.Program.Name;
				MinimumCreditsValue = Model.Program.MinimumCredits.ToString();
				SelectRequiredCoursesModel.SelectedCourses = Model.Program.RequiredCourses.ToList();
				SelectRecommendedCoursesModel.SelectedCourses = Model.Program.RecommendedCourses.ToList();
			}
			else
			{
				NameValue = "";
				MinimumCreditsValue = "";
				SelectRequiredCoursesModel.SelectedCourses = new List<Course>();
				SelectRecommendedCoursesModel.SelectedCourses = new List<Course>();
			}
		}
	}
}